#include "adc.h"

void adc_init(void)
{
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
    RCC_ADCCLKConfig(RCC_PCLK2_Div6);
    GPIO_InitTypeDef GPIO_init_struct;
    ADC_InitTypeDef adc_init_struct;
    GPIO_init_struct .GPIO_Mode = GPIO_Mode_AIN;
    GPIO_init_struct .GPIO_Pin = GPIO_Pin_1 | GPIO_Pin_4;

    adc_init_struct .ADC_ContinuousConvMode = DISABLE;
    adc_init_struct .ADC_DataAlign = ADC_DataAlign_Right;
    adc_init_struct .ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;
    adc_init_struct .ADC_Mode = ADC_Mode_Independent;
    adc_init_struct .ADC_NbrOfChannel = ADC_Channel_1 | ADC_Channel_4;
    adc_init_struct .ADC_ScanConvMode = DISABLE;

    GPIO_Init(GPIOA,&GPIO_init_struct);
    ADC_Init(ADC1, &adc_init_struct);
    ADC_Cmd(ADC1, ENABLE);
    ADC_ResetCalibration(ADC1); /*复位校准*/
    while (ADC_GetResetCalibrationStatus(ADC1));
    ADC_StartCalibration(ADC1); /*开始校准*/
    while(!ADC_GetCalibrationStatus(ADC1));
         
}




uint16_t adc_convert(ADC_TypeDef* ADCx,uint8_t ADC_Channel)
{      
    ADC_RegularChannelConfig(ADC1,ADC_Channel, 1, ADC_SampleTime_1Cycles5);
    ADC_SoftwareStartConvCmd(ADCx,ENABLE);
    while(!ADC_GetFlagStatus(ADCx, ADC_FLAG_EOC));
    return ADC_GetConversionValue(ADCx);
}
